<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 6.2.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">
  <meta name="google-site-verification" content="kYQuniuaRZuL9fq0DATfL7extAwcqWG6Ki286ZMcpak">
  <meta name="baidu-site-verification" content="code-MRqS7DOO9x">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css" integrity="sha256-DfWjNxDkM94fVBWx1H5BMMp0Zq7luBlV8QRcSES7s+0=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"blogwxb.cn","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.11.1","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":null},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":true,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":"gitalk","storage":true,"lazyload":false,"nav":null,"activeClass":"gitalk"},"stickytabs":false,"motion":{"enable":false,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="最近老是抖音老是刷到一个小孩子玩数字华容道特别厉害，我感觉挺有趣的，就做了一个数字华容道，上班摸鱼可以玩，哈哈哈哈哈。">
<meta property="og:type" content="article">
<meta property="og:title" content="用pixijs实现数字华容道游戏">
<meta property="og:url" content="https://blogwxb.cn/%E7%94%A8pixijs%E5%AE%9E%E7%8E%B0%E6%95%B0%E5%AD%97%E5%8D%8E%E5%AE%B9%E9%81%93%E6%B8%B8%E6%88%8F/index.html">
<meta property="og:site_name" content="会飞的猪9527">
<meta property="og:description" content="最近老是抖音老是刷到一个小孩子玩数字华容道特别厉害，我感觉挺有趣的，就做了一个数字华容道，上班摸鱼可以玩，哈哈哈哈哈。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://resource.blogwxb.cn/digitalHuarongRoad/screen-recording.gif">
<meta property="article:published_time" content="2022-12-12T02:48:31.000Z">
<meta property="article:modified_time" content="2022-12-12T02:50:06.368Z">
<meta property="article:author" content="wxb">
<meta property="article:tag" content="JavaScript">
<meta property="article:tag" content="pixi.js">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://resource.blogwxb.cn/digitalHuarongRoad/screen-recording.gif">


<link rel="canonical" href="https://blogwxb.cn/%E7%94%A8pixijs%E5%AE%9E%E7%8E%B0%E6%95%B0%E5%AD%97%E5%8D%8E%E5%AE%B9%E9%81%93%E6%B8%B8%E6%88%8F/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://blogwxb.cn/%E7%94%A8pixijs%E5%AE%9E%E7%8E%B0%E6%95%B0%E5%AD%97%E5%8D%8E%E5%AE%B9%E9%81%93%E6%B8%B8%E6%88%8F/","path":"用pixijs实现数字华容道游戏/","title":"用pixijs实现数字华容道游戏"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>用pixijs实现数字华容道游戏 | 会飞的猪9527</title>
  





  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
  <!-- Google Analytics -->
  <script>
  window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
  ga('create', 'UA-XXXXX-Y', 'auto');
  ga('send', 'pageview');
  </script>
  <script async src='https://www.google-analytics.com/analytics.js'></script>
  <!-- End Google Analytics -->

  <!-- Baidu Analytics -->
  <script async src = 'https://zz.bdstatic.com/linksubmit/push.js'></script>
  <!-- End Baidu Analytics -->

<link rel="alternate" href="/atom.xml" title="会飞的猪9527" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">会飞的猪9527</p>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签<span class="badge">32</span></a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类<span class="badge">5</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档<span class="badge">91</span></a></li><li class="menu-item menu-item-commonweal"><a href="/404" rel="section"><i class="fa fa-heartbeat fa-fw"></i>公益 404</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%AE%9E%E7%8E%B0%E6%80%9D%E8%B7%AF"><span class="nav-number">1.</span> <span class="nav-text">实现思路</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%B8%B8%E6%88%8F%E6%80%8E%E4%B9%88%E5%88%9D%E5%A7%8B%E5%8C%96%EF%BC%9F"><span class="nav-number">1.1.</span> <span class="nav-text">游戏怎么初始化？</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E6%B8%B8%E6%88%8F%E6%98%B5%E7%A7%B0%E6%80%8E%E4%B9%88%E9%9A%8F%E6%9C%BA%EF%BC%9F"><span class="nav-number">2.</span> <span class="nav-text">游戏昵称怎么随机？</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="wxb"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">wxb</p>
  <div class="site-description" itemprop="description">主要是分享自己在Web前端的一些入的坑，脱的坑的经历，也会是分享一些Node后端的知识。</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">91</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">5</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">32</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author site-overview-item animated">
      <span class="links-of-author-item">
        <a href="https://github.com/dearDreamWeb" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;dearDreamWeb" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>


  <div class="links-of-blogroll site-overview-item animated">
    <div class="links-of-blogroll-title"><i class="fa fa-globe fa-fw"></i>
      推荐
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="https://www.ruanyifeng.com/blog/" title="https:&#x2F;&#x2F;www.ruanyifeng.com&#x2F;blog&#x2F;" rel="noopener" target="_blank">阮一峰的网络日志</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://nav.blogwxb.cn/" title="https:&#x2F;&#x2F;nav.blogwxb.cn&#x2F;" rel="noopener" target="_blank">轩辕导航</a>
        </li>
    </ul>
  </div>

        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

  <a href="https://github.com/dearDreamWeb" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://blogwxb.cn/%E7%94%A8pixijs%E5%AE%9E%E7%8E%B0%E6%95%B0%E5%AD%97%E5%8D%8E%E5%AE%B9%E9%81%93%E6%B8%B8%E6%88%8F/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="wxb">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="会飞的猪9527">
      <meta itemprop="description" content="主要是分享自己在Web前端的一些入的坑，脱的坑的经历，也会是分享一些Node后端的知识。">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="用pixijs实现数字华容道游戏 | 会飞的猪9527">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          用pixijs实现数字华容道游戏
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>
      

      <time title="创建时间：2022-12-12 10:48:31 / 修改时间：10:50:06" itemprop="dateCreated datePublished" datetime="2022-12-12T10:48:31+08:00">2022-12-12</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%89%8D%E7%AB%AF/" itemprop="url" rel="index"><span itemprop="name">前端</span></a>
        </span>
    </span>

  
    <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">阅读次数：</span>
      <!-- <span id="busuanzi_value_page_pv"></span> -->
      <span id="views_text"></span>
    </span>
</div>


        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <p>最近老是抖音老是刷到一个小孩子玩数字华容道特别厉害，我感觉挺有趣的，就做了一个数字华容道，上班摸鱼可以玩，哈哈哈哈哈。  </p>
<span id="more"></span>
<img data-src="https://resource.blogwxb.cn/digitalHuarongRoad/screen-recording.gif">

<p><a target="_blank" rel="noopener" href="https://dhr.blogwxb.cn/">项目预览地址</a><br><a target="_blank" rel="noopener" href="https://github.com/dearDreamWeb/digital-huarong-road">项目地址</a> 喜欢的话请给个 star⭐️</p>
<p>整体的样式是采用了<code>像素风格</code>。<br>功能：</p>
<ul>
<li>游戏模式可以自定义选择难度，比如 3X3、4X4、5X5 等</li>
<li>设置的有各个模式的排行榜，可以看到排名</li>
<li>用户昵称是随机的，如果不满意可以更换昵称</li>
</ul>
<h1 id="实现思路"><a href="#实现思路" class="headerlink" title="实现思路"></a>实现思路</h1><p>核心的部分肯定是游戏交互的部分了，这里采用的是二维数组的方式实现的。<br><code>二维数组</code>：主要是为了每个模式的初始化的时候，以二维数组的形式为默认值，为了后面区分<code>行数</code>和<code>列数</code>。需要用到两个二维数组，一个是为了初始化的值<code>initData</code>，一个是当前游戏中的值<code>curData</code>。<br>如：3X3 的模式初始化 initData</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">[</span><br><span class="line">    [1,2,3],</span><br><span class="line">    [4,5,6],</span><br><span class="line">    [7,8,null]</span><br><span class="line">]</span><br></pre></td></tr></table></figure>

<p>由于是用的 pixijs 去生成的方格，点击事件获取的是该方格的<code>数值</code>，如：1,2,3,4,5,6 等等。根据点击的方格数值判断该方格<code>四周（上下左右）</code>有没有<code>null</code>值，有的话就进行交换，得到新的<code>curData</code>。这里判断是有些麻烦的。</p>
<p>例子：<br>如果当前点击的是 3，当前模式的行数(<code>rows</code>)为 3，列数(<code>columns</code>)为 3<br>当前的布局(<code>curData</code>)为：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">[</span><br><span class="line">    [2,6,3],</span><br><span class="line">    [1,null,5],</span><br><span class="line">    [7,4,8]</span><br><span class="line">]</span><br></pre></td></tr></table></figure>

<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 先获取点击数字在 curData中的位置</span></span><br><span class="line"><span class="keyword">const</span> curDataFlat = curData.<span class="title function_">flat</span>(); <span class="comment">// 数组扁平化处理，二维数组转成一维数组</span></span><br><span class="line"><span class="keyword">const</span> index = curDataFlat.<span class="title function_">indexOf</span>(<span class="string">&#x27;3&#x27;</span>); <span class="comment">// index 为2</span></span><br><span class="line"><span class="comment">// 要移动的方向</span></span><br><span class="line"><span class="keyword">let</span> direction = <span class="string">&#x27;&#x27;</span>;</span><br><span class="line"><span class="comment">// 要交换的位置</span></span><br><span class="line"><span class="keyword">let</span> newIndex = index;</span><br><span class="line"><span class="comment">// 左方向的判断</span></span><br><span class="line"><span class="keyword">if</span> (curDataFlat[index - <span class="number">1</span>] === <span class="literal">null</span>) &#123;</span><br><span class="line">  <span class="keyword">if</span> (index % columns !== <span class="number">0</span>) &#123;</span><br><span class="line">    direction = <span class="string">&#x27;left&#x27;</span>;</span><br><span class="line">    newIndex = index - <span class="number">1</span>;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="comment">// 右方向的判断</span></span><br><span class="line">&#125; <span class="keyword">else</span> <span class="keyword">if</span> (curDataFlat[index + <span class="number">1</span>] === <span class="literal">null</span>) &#123;</span><br><span class="line">  <span class="keyword">if</span> (index % columns !== columns - <span class="number">1</span>) &#123;</span><br><span class="line">    direction = <span class="string">&#x27;right&#x27;</span>;</span><br><span class="line">    newIndex = index + <span class="number">1</span>;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="comment">// 下方向的判断</span></span><br><span class="line">&#125; <span class="keyword">else</span> <span class="keyword">if</span> (curDataFlat[index + columns] === <span class="literal">null</span>) &#123;</span><br><span class="line">  direction = <span class="string">&#x27;bottom&#x27;</span>;</span><br><span class="line">  newIndex = index + columns;</span><br><span class="line">  <span class="comment">// 上方向的判断</span></span><br><span class="line">&#125; <span class="keyword">else</span> <span class="keyword">if</span> (curDataFlat[index - columns] === <span class="literal">null</span>) &#123;</span><br><span class="line">  direction = <span class="string">&#x27;top&#x27;</span>;</span><br><span class="line">  newIndex = index - columns;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>每次交换之后<code>curData</code>都要和<code>initData</code>进行对比，如果一样代表成功了，游戏结束。</p>
<h2 id="游戏怎么初始化？"><a href="#游戏怎么初始化？" class="headerlink" title="游戏怎么初始化？"></a>游戏怎么初始化？</h2><p>一开始我想的很简单，拿<code>3X3</code>的模式举例子就是拿<code>1-8</code>的数字随机取。<br>数组： [1,2,3,4,5,6,7,8]<br>第一次随机拿到了<code>3</code>就放第一个格子里,这时候数组变成了[1,2,4,5,6,7,8]<br>第二次随机拿到了<code>5</code>放第二个格子中,这时候数组变成了[1,2,4,6,7,8]<br>…<br>依次类推把格子填满了。<br>本来以为这样就大功告成了。后面发现了一个很致命的<code>问题</code>：<code>有时候这样的随机布局根本就无法通关</code>。<br>举一个最简单的例子：<br>随机的布局是这样子的：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">[</span><br><span class="line">    [1,2,3],</span><br><span class="line">    [4,5,6],</span><br><span class="line">    [8,7,null]</span><br><span class="line">]</span><br></pre></td></tr></table></figure>

<p>这种是无法通关的，emm……。</p>
<p>换个思路：<br>换个玩魔方的思路，魔方是怎么随机的，是先将一个复原好的魔方进行随机旋转打乱的。ok，用这个思路就可以进行数字华容道的随机布局了。<br>具体思路：</p>
<ol>
<li>一开始先用完成好的布局：</li>
</ol>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">[</span><br><span class="line">    [1,2,3],</span><br><span class="line">    [4,5,6],</span><br><span class="line">    [7,8,null]</span><br><span class="line">]</span><br></pre></td></tr></table></figure>

<ol start="2">
<li><code>随机上下左右移动null值</code>，这样随机移动几十步或者上百步就可以打乱初始化的布局了，每次的随机布局也都能通关了。</li>
</ol>
<h1 id="游戏昵称怎么随机？"><a href="#游戏昵称怎么随机？" class="headerlink" title="游戏昵称怎么随机？"></a>游戏昵称怎么随机？</h1><p>一开始我想的是随机拿到汉字，这里是可以通过 Unicode 编码去实现的。<br>汉字的范围是 <code>4e00-9fa5</code>,这些编码前面加上<code>0x</code>就是能得到十进制的数值了，再通过<code>toString(16)</code>转换成十六进制<code>4e00</code>，最后将<code>4e00</code>加上<code>\u</code>，得到<code>\u4e00</code>，通过<code>unescape</code>方法得到该字符。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">randomAccess</span>(<span class="params">min, max</span>) &#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="title class_">Math</span>.<span class="title function_">floor</span>(<span class="title class_">Math</span>.<span class="title function_">random</span>() * (min - max) + max);</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 解码</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">decodeUnicode</span>(<span class="params">str</span>) &#123;</span><br><span class="line">  <span class="comment">//Unicode显示方式是\u4e00</span></span><br><span class="line">  str = <span class="string">&#x27;\\u&#x27;</span> + str;</span><br><span class="line">  str = str.<span class="title function_">replace</span>(<span class="regexp">/\\/g</span>, <span class="string">&#x27;%&#x27;</span>);</span><br><span class="line">  <span class="comment">//转换中文</span></span><br><span class="line">  str = <span class="built_in">unescape</span>(str);</span><br><span class="line">  <span class="comment">//将其他受影响的转换回原来</span></span><br><span class="line">  str = str.<span class="title function_">replace</span>(<span class="regexp">/%/g</span>, <span class="string">&#x27;\\&#x27;</span>);</span><br><span class="line">  <span class="keyword">return</span> str;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">translateName</span>(<span class="params"></span>) &#123;</span><br><span class="line">  <span class="comment">// 中文</span></span><br><span class="line">  <span class="keyword">let</span> randomArr = [<span class="number">0x4e00</span>, <span class="number">0x9fa5</span>];</span><br><span class="line">  <span class="keyword">let</span> unicodeNum = <span class="string">&#x27;&#x27;</span>;</span><br><span class="line">  unicodeNum = <span class="title function_">randomAccess</span>(randomArr[<span class="number">0</span>], randomArr[<span class="number">1</span>]).<span class="title function_">toString</span>(<span class="number">16</span>);</span><br><span class="line">  <span class="keyword">let</span> result = <span class="title function_">decodeUnicode</span>(unicodeNum);</span><br><span class="line"></span><br><span class="line">  <span class="keyword">return</span> result;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 获取到中文</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title function_">translateName</span>());</span><br></pre></td></tr></table></figure>

<p>这样子虽然能得到中文，但是会得到很多偏僻字，比如<code>鈠 鑙</code>，为了防止这些偏僻字的出现，想到了可以按照中文的<code>笔画数</code>划分，一般来说笔画少的生僻字是会少很多的。<br><code>cnchar</code>这个 npm 包就是<code>可以识别汉字的笔画数的</code>。有了这个 npm 包，事半功倍了起来，当汉字的笔画数大于<code>10</code>就重新获取汉字，直到笔画数<code>少于10</code>为止。<br>后面我又加入了一些<code>日文</code>、<code>符合</code>和<code>阿拉伯数字</code>。</p>
<p>————–END——————-</p>

    </div>

    
    
    
      


    <footer class="post-footer">
          <div class="reward-container">
  <div></div>
  <button>
    赞赏
  </button>
  <div class="post-reward">
      <div>
        <img src="/images/wechatpay.png" alt="wxb 微信">
        <span>微信</span>
      </div>
      <div>
        <img src="/images/alipay.png" alt="wxb 支付宝">
        <span>支付宝</span>
      </div>

  </div>
</div>

          <div class="followme">
  <span>欢迎关注我的其它发布渠道</span>

  <div class="social-list">

      <div class="social-item">
        <a target="_blank" class="social-link" href="/atom.xml">
          <span class="icon">
            <i class="fa fa-rss"></i>
          </span>

          <span class="label">RSS</span>
        </a>
      </div>
  </div>
</div>

          <div class="post-tags">
              <a href="/tags/JavaScript/" rel="tag"># JavaScript</a>
              <a href="/tags/pixi-js/" rel="tag"># pixi.js</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/pixijs%EF%BC%8Bwebgl%E5%AE%9E%E7%8E%B0%E5%9F%8E%E5%B8%82%E4%BA%A4%E9%80%9A%E6%A8%A1%E6%8B%9F/" rel="prev" title="pixijs＋webgl实现城市交通模拟">
                  <i class="fa fa-chevron-left"></i> pixijs＋webgl实现城市交通模拟
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/node%E7%9A%84%E8%B0%83%E8%AF%95%E6%96%B9%E6%B3%95-Inspector%E8%B0%83%E8%AF%95/" rel="next" title="node的调试方法-Inspector调试">
                  node的调试方法-Inspector调试 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






    <div class="comments gitalk-container"></div>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">wxb</span>
</div>
<div class="busuanzi-count">
    <span class="post-meta-item" id="busuanzi_container_site_uv">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <!-- <span id="busuanzi_value_site_uv"></span> -->
        <span id="uv-views"></span>
      </span>
    </span>
    <span class="post-meta-item" id="busuanzi_container_site_pv">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <!-- <span id="busuanzi_value_site_pv"></span> -->
        <span id="pv-views"></span>
      </span>
    </span>
</div><script src="/js/diy_footer.js"></script>
  <div class="powered-by">
    <div style="display: inline-flex;align-items: center;">
      <img src="/images/copyrihgt.png" alt="wxb 微信" with='20' height='20' style="margin-right: 4px;">
      <a href="https://beian.miit.gov.cn/" target="_blank">浙ICP备2022027687号</a>
    </div>
  </div>

    </div>
  </footer>

  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.5.0/pjax.min.js" integrity="sha256-3NkoLDrmHLTYj7csHIZSr0MHAFTXth7Ua/DDt4MRUAg=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/medium-zoom/1.0.6/medium-zoom.min.js" integrity="sha256-EdPgYcPk/IIrw7FYeuJQexva49pVRZNmt3LculEr7zM=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/lozad.js/1.16.0/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  
<script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.0/search.js" integrity="sha256-vXZMYLEqsROAXkEw93GGIvaB2ab+QW6w3+1ahD9nXXA=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>





  
  <script data-pjax async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>




<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/gitalk/1.7.2/gitalk.css" integrity="sha256-AJnUHL7dBv6PGaeyPQJcgQPDjt/Hn/PvYZde1iqfp8U=" crossorigin="anonymous">

<script class="next-config" data-name="gitalk" type="application/json">{"enable":true,"github_id":"dearDreamWeb","repo":"blogwxb.github.io","client_id":"86685a6104f8e6ba5827","client_secret":"7cf015e84bbe755bde20c8aca767977e316304ad","admin_user":"dearDreamWeb","distraction_free_mode":true,"proxy":"https://cors-anywhere.azm.workers.dev/https://github.com/login/oauth/access_token","language":"zh-CN","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/gitalk/1.7.2/gitalk.min.js","integrity":"sha256-Pmj85ojLaPOWwRtlMJwmezB/Qg8BzvJp5eTzvXaYAfA="},"path_md5":"f15abb9e99905fa8215976caedd7a0b5"}</script>
<script src="/js/third-party/comments/gitalk.js"></script>

</body>
</html>
